package com.gmj.controller;


import com.baomidou.mybatisplus.extension.service.IService;
import com.gmj.entity.BookLaboratory;
import com.gmj.entity.Laboratory;
import com.gmj.entity.Teacher;
import com.gmj.service.IBookLaboratoryService;
import com.gmj.service.ILaboratoryService;
import com.gmj.service.ITeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gmj
 * @since 2022-01-29
 */
@RestController
@RequestMapping("/bookLaboratory")
public class BookLaboratoryController {
    @Autowired
    private IBookLaboratoryService bs;
    @Autowired
    private ITeacherService ts;
    @Autowired
    private ILaboratoryService ls;
    @GetMapping("/selectAll")
    public List<BookLaboratory> selectAll(){
        List<Teacher> teacherList = ts.list();
        List<Laboratory> laboratoryList = ls.list();
        List<BookLaboratory> list = bs.list();
        list = list.stream().peek((bookLaboratory) -> {
            Integer lid = bookLaboratory.getLid();
            Integer tid = bookLaboratory.getTid();
            Teacher tea = teacherList.stream().filter((teacher -> {
                return teacher.getId() == tid;
            })).collect(Collectors.toList()).get(0);
            bookLaboratory.setTeacher(tea);
            Laboratory lab = laboratoryList.stream().filter((laboratory -> {
                return laboratory.getId() == lid;
            })).collect(Collectors.toList()).get(0);
            bookLaboratory.setLaboratory(lab);
        }).collect(Collectors.toList());
        return list;
    }
    @PostMapping("/add")
    public void add(@RequestBody BookLaboratory bookLaboratory){
        bookLaboratory.setBookDate(LocalDateTime.now());
        bookLaboratory.setState(0);
        bs.save(bookLaboratory);
    }
    @PutMapping("/updateState")
    public void updateState(@RequestBody BookLaboratory bl){
        bs.updateById(bl);
    }
}

